let element = layui.element, table = layui.table, $ = layui.jquery, id = getQueryVariable("id");

let recordMap = [];
if (id!=""){
    $.ajax({
        url: "inquiry/selectRecordByInId/"+id,
        success: function (res) {
            if (res.status == 200) {
                if (res.data!=null){
                    recordMap = res.data;
                    // const map = new Map(Object.entries(recordMap));
                    tableContext(recordMap);
                }
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}

function tableContext(recordMap){
    let newLi = "";
    let newDiv = "";
    let num = 0;
    for(let key in recordMap){
        let li = "";
        let div = "";
        let isCheck = num==0?"layui-this":"";
        let isShow  = num==0?"layui-show":"";
        li = '<li class="'+isCheck+'">记录' + (num + 1) + '</li>'
        div = ' <div  class="layui-tab-item '+isShow+'">\n' +
                '  <table class="layui-table" id="record_' + num + '" lay-filter="record_' + num + '"></table>\n' +
                '  <div class="padding text-right">\n' +
                    // '  <button type="button" class="layui-btn layui-btn-normal setmasterrecord" data-id="' + recordMap[key][0].frequency + '">设为主记录</button>\n' +
                '  </div>\n' +
            '  </div>';
        newLi += li;
        newDiv += div;
        num++;
    }
    $(".layui-tab-title").html(newLi);
    $(".layui-tab-content").html(newDiv);
    $(".setmasterrecord").on("click", function () {
        let frequency = $(this).attr("data-id");
        layer.confirm('确认设置主记录?', function (index) {
            $.ajax({
                type: 'POST',
                // data:{"id":id},
                url: 'inquiry/setMasterRecordByInId/' + id+"/"+frequency,
                dataType: "json",
                contentType: 'application/json;charset=utf-8',
                async: false,
                success: function (res) {
                    if (res.status == 200) {
                        if (res.data) {
                            layer.msg("设置主记录成功", {icon: 6});
                            setTimeout(function () {
                                //保存成功则并关闭此页面
                                let index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }, 1000);
                        } else {
                            layer.msg("设置主记录失败!");
                        }
                    } else {
                        layer.msg("请求失败!");
                    }
                },
                error: function (data) {
                    layer.msg("设置主记录失败!");
                }
            });
        });
    });

    element.render();
    let numNew = 0;
    for(let key in recordMap){
        table.render({
            elem: '#record_' + numNew,
            id: 'record_' + numNew + "tableId",
            data: recordMap[key],
            cols: [[
                {field: 'productNo', title: '产品编码', sort: true, width: 120},
                {field: 'specNo', title: '规格编码', sort: true, width: 120},
                {field: 'normExplain', title: '规格说明'},
                {field: 'price', title: '理论单价', sort: true, width: 120},
                {field: 'amount', width: 80, title: '数量', sort: true},
                {field: 'priceSubtotal', width: 100, title: '金额小计', sort: true},
                {field: 'factory', title: '询价工厂', sort: true, width: 120},
                {field: 'cost', title: '工厂售价', sort: true, width: 120},
                {field: 'frequency', title: '次数', width: 120},
            ]],
            page: true,
            height: $(document).height() - 220
        });
        numNew++;
    }
}